package org.medicalinstrument.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.medicalinstrument.bean.PurchaseOrderDetail;
import org.medicalinstrument.bean.PurchaseOrderMaster;
import org.medicalinstrument.bean.ShoppingCart;
import org.medicalinstrument.bean.ShoppingCart_VO;
import org.medicalinstrument.dao.ShoppingCartDao;
import org.medicalinstrument.database.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

public class ShoppingCartDaoImpl implements ShoppingCartDao {
    private QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
    ShoppingCart cart = new ShoppingCart();
    /**
     *@Author 谢浩天
     *@Description TODO
     *@Date 18:02 2019/3/26
     *@Modify by
     *@Version 1.0
     */
    public Boolean add(ShoppingCart cart) throws SQLException {

        String sql = "insert into shoppingcart(custPhone,accountNO,productQR,buyAmount,totalPrice)values (?,?,?,?,?)";
        int update = runner.update(sql, cart.getCustPhone(), cart.getAccountNo(), cart.getProductQR(), cart.getBuyAmount(), cart.getTotalPrice());

        return update > 0;
    }
    /**
     *@Author 谢浩天
     *@Description use for reduce or plus the shopping number
     *@Date 17:23 2019/3/25
     *@Modify by  cirly   --
     *@Version 1.0
     */
    public Boolean update(ShoppingCart cart) throws SQLException {
        String sql = "update shoppingcart set buyAmount = ? where  productQR = ?";
    //System.out.println(cart);
        int update = runner.update(sql, cart.getBuyAmount(), cart.getProductQR());
        return update > 0;

    }

    public Boolean delete(List qrList) throws SQLException {
        String sql = "delete from shoppingcart where  productQR = ?";
        int update = 0;
        for(int i = 0 ; i < qrList.size() ; i ++ ){
            String productQR = (String) qrList.get(i);
            update =  runner.update(sql,productQR);
        }


        return update > 0;
    }
  /**
   *@Author 谢浩天
   *@Description TODO
   *@Date 19:24 2019/3/19
   *@Modify by  cirly      --增加参数
   *@Version 1.0
   */
    @Override
    public List<ShoppingCart_VO> getAll(String accountNo) throws SQLException {
        String sql = "select * from productCart_view  where accountNo=?";
        List<ShoppingCart_VO> cart = runner.query(sql,new BeanListHandler<ShoppingCart_VO>(ShoppingCart_VO.class),accountNo);
       if(cart.size() == 0 ){
           return null;
       }else {
           // System.out.println(cart.size());
           for (int i = 0; i < cart.size(); i++) {
//            System.out.println((cart.get(i).getProductQR()));
               cart.get(i).setProductNumber(getQR(accountNo));
               //System.out.println(cart.get(i));
           }
           return cart;
       }


    }


    public int getQR(String accountNo) throws SQLException {
        String sql = "select * from productCart_view  where accountNo='"+accountNo+"'";

//        String sql = "select productName from product  where productQR="+productQR;
       //System.out.println(sql);
        String productQR = runner.query(sql, new ScalarHandler<String>(3));
       System.out.println("QR:  "+productQR);
    return getProductNumber(productQR);
    }

    public int getProductNumber(String productQR) throws SQLException {
        //System.out.println(productQR);
        String sql = "select * from product  where productQR="+productQR;
//        String sql = "select productName from product  where productQR="+productQR;
        // System.out.println(sql);
        String productName ,productType ;
        productName = runner.query(sql, new ScalarHandler<String>(3));
        //  List<String> list =   runner.query(sql, new ColumnListHandler<String>(productQR));
        //System.out.println(list.get(0));
        //System.out.println(productName);
        sql = "select * from product where productQR="+productQR;
        productType = runner.query(sql, new ScalarHandler<String>(2));
        sql = "select count(*) from product where productName='"+productName+"' and productType = '"+productType+"'";
        Object number = runner.query(sql, new ScalarHandler<>());
        Integer y = Integer.parseInt(number.toString());
         System.out.println("number:"+y);
        return y;

    }
    /**
     *@Author cirly
     *@Description click settlement
     *@Date 18:02 2019/3/26
     *@Modify by
     *@Version 1.0
     */
    public String buy(List<PurchaseOrderMaster> pmList, List<PurchaseOrderDetail> pdList) throws SQLException {
        /**
         * 此方法写法很不合理，没有回滚机制，变量定义的很微妙，需要改进
         */

        String sql = "insert into purchaseordermaster values(?,?,?,?,?,?,?,?,?,?)";
        String sql2 = "insert into purchaseorderdetail values(?,?,?,?,?,?,?,?)";
        int pm_count = 0,pd_count = 0;
        PurchaseOrderMaster pm = pmList.get(0);

        //System.out.println(pm);
        pm_count = runner.update(sql,pm.getOrderId(),pm.getCustPhone(),pm.getAccountNo(),pm.getOrderTime(),
                pm.getConsigeeAddr(),pm.getGetOrConsigee(),pm.getBuyPrice(),
                pm.getRealPayPrice(),pm.getOrderState(),pm.getMessage());
        if(pm_count < 0 ){
            return "insert_error";
        }else{
            for(int i = 0 ; i < pdList.size() ; i ++){
                PurchaseOrderDetail pd = pdList.get(i);

                //System.out.println(pd);
                pd_count += runner.update(sql2,pd.getCustPhone(),pd.getAccountNo(),pd.getOrderTime(),pd.getProductQR(),
                        pd.getBuyTime(),pd.getBuyNumber(),pd.getPrice(),pd.getOrderID());
            }
        }
        if(pd_count <= 0){
            return "insert_error";
        }else{
            return "insert_success";
        }

    }

    /**
     *@Author cirly
     *@Description judge whether there be the product infos in the shoppingcart before insert
     *@Date 13:04 2019/3/24
     *@Modify by
     *@Version 1.0
     */
    public ShoppingCart_VO isExist (String productQR) throws SQLException {
        String sql = "select * from shoppingcart  where productQR=?";
        ShoppingCart_VO cart = runner.query(sql,new BeanHandler<>(ShoppingCart_VO.class),productQR);
        return cart;
    }

}
